package com.zc.service;

import com.zc.dao.UserMapper;
import com.zc.pojo.Staff;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @description:
 * @author: cong
 * @create: 2024/12/27
 */
@Service
@Slf4j
public class StaffService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 根据用户ID选择用户信息
     * 该方法使用了Spring的@Cacheable注解，用于缓存方法的返回值
     * 当方法参数（id）相同时，会从缓存中读取数据，而不是执行方法
     *
     * @param id 用户ID，用于查询用户信息
     * @return Staff 对象，包含用户信息
     */
    @Cacheable(value = "staff",cacheManager = "cacheManager", keyGenerator = "keyGenerator")
    public Staff selectUserById(Integer id) {
        log.info("查询用户信息");
        Staff staff = userMapper.selectUserById(id);
        log.info("查询用户信息结束");
        return staff;
    }

    @CachePut(value = "staff", key = "#staff.id")
    public int updateStaff(Staff staff) {
        return userMapper.updateStaff(staff);
    }

    @Cacheable(value = "testCache")
    public String testCache() {
        return "test cache";
    }
}
